package com.imay.live.activity.family.controller;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.R;
import com.imay.live.adapter.family.FamilyHomeAdapter;
import com.imay.live.adapter.family.FamilyAcceptAdapter;
import com.imay.live.model.FamilyApplyUserInfo;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.AnimationUtil;
import com.imay.live.utils.RecyclerViewUtil;
import com.imay.live.utils.ToastUtil;
import com.imay.live.utils.ViewUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/7 0007.
 */

public class FamilyAcceptController extends BaseFamilyController {

    RecyclerView rv_pass_user;
    FamilyAcceptAdapter mAdapter;
    TextView tv_family_pass, tv_family_reject;

    List<MultiItemEntity> mDatas;

    public static final int REJECT = 2;
    public static final int PASS = 3;
    private ArrayList<String> names;
    private ArrayList<Integer> ids;

    @Override
    protected void initView() {
        rv_pass_user = ViewUtil.findView(activity, R.id.rv_pass_user);
        tv_family_pass = ViewUtil.findView(activity, R.id.tv_family_pass);
        tv_family_reject = ViewUtil.findView(activity, R.id.tv_family_reject);
        RecyclerViewUtil.setLinearLayoutManager(activity, rv_pass_user);

        mAdapter = new FamilyAcceptAdapter();
        View view = new View(activity);
        view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, (int) activity.getResources().getDimension(R.dimen.size_115dp)));
        view.setBackgroundColor(Color.WHITE);
        mAdapter.addFooterView(view);
        mAdapter.setEmptyView(LayoutInflater.from(activity).inflate(R.layout.view_load_layout, null));
        rv_pass_user.setAdapter(mAdapter);

        names = new ArrayList<>();
        ids = new ArrayList<>();

        initListener();
    }

    private void initListener() {


        if (activity instanceof View.OnClickListener) {
            tv_family_pass.setOnClickListener((View.OnClickListener) activity);
            tv_family_reject.setOnClickListener((View.OnClickListener) activity);
        }

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.iv_family_accept:
                        if (adapter.getItem(position) instanceof FamilyApplyUserInfo) {
                            FamilyApplyUserInfo info = (FamilyApplyUserInfo) adapter.getItem(position);
                            if (!info.isSelect()) {
                                info.setSelect(true);
                                view.setSelected(true);
                                ids.add((Integer) info.getId());
                                names.add(info.getNick());
                            } else {
                                info.setSelect(false);
                                view.setSelected(false);
                                ids.remove((Integer) info.getId());
                                names.remove(info.getNick());
                            }
                            buttomShow();
                        }
                        break;
                }
            }
        });
    }

    private void buttomShow() {
        if (ids.isEmpty()) {
            if (tv_family_pass.getVisibility() == View.VISIBLE
                    && tv_family_reject.getVisibility() == View.VISIBLE) {
                AnimationUtil.alphaInAnimation(tv_family_pass, 1F, 0F, 0);
                tv_family_pass.setVisibility(View.GONE);
                AnimationUtil.alphaInAnimation(tv_family_reject, 1F, 0F, 0);
                tv_family_reject.setVisibility(View.GONE);
            }
        } else {
            if (tv_family_pass.getVisibility() == View.GONE
                    && tv_family_reject.getVisibility() == View.GONE) {
                AnimationUtil.alphaInAnimation(tv_family_pass, 0F, 1F, 0);
                tv_family_pass.setVisibility(View.VISIBLE);
                AnimationUtil.alphaInAnimation(tv_family_reject, 0F, 1F, 0);
                tv_family_reject.setVisibility(View.VISIBLE);
            }
        }
    }

    public void selectAll() {
        for (MultiItemEntity item : mDatas) {
            if (item instanceof FamilyApplyUserInfo) {
                FamilyApplyUserInfo info = (FamilyApplyUserInfo) item;
                if (!info.isSelect()) {
                    info.setSelect(true);
                    ids.add(info.getId());
                    names.add(info.getNick());
                }
            }
        }
        mAdapter.notifyDataSetChanged();
        buttomShow();
    }

    // 通过
    public String passUsers() {
        return loadCheckApplyUsers(PASS);
    }

    // 拒绝
    public String rejectUsers() {
        return loadCheckApplyUsers(REJECT);
    }

    /**
     * @param status 2:拒绝，3:通过
     * @return
     */
    private String loadCheckApplyUsers(int status) {
        if (ids.isEmpty()) {
            ToastUtil.show(activity, R.string.str_family_apply_toast4);
            return null;
        }
        if (activity instanceof ProtoCallBack) {
            return ProtoRequestHelper.task(ProtoHttpFactory.checkJoinApply(ids, status)).setTag(activity.hashCode()).request((ProtoCallBack) activity);
        }
        return null;
    }

    public void setUserData(List<FamilyApplyUserInfo> userData) {
        List<MultiItemEntity> datas = new ArrayList<>();
        datas.addAll(userData);
        if (mDatas == null) {
            mDatas = datas;
        } else {
            mDatas.clear();
            mDatas.addAll(datas);
        }
        mAdapter.setNewData(mDatas);
        buttomShow();
    }

    public void passToast() {
        if (names.isEmpty()) {
            return;
        }
        if (names.size() == 1) {
            ToastUtil.show(activity, activity.getString(R.string.str_family_apply_toast1, names.get(0)));
        } else {
            ToastUtil.show(activity, activity.getString(R.string.str_family_apply_toast, names.get(0)));
        }
        resetDatas();
    }

    public void rejectToast() {
        if (names.isEmpty()) {
            return;
        }
        if (names.size() == 1) {
            ToastUtil.show(activity, activity.getString(R.string.str_family_apply_toast3, names.get(0)));
        } else {
            ToastUtil.show(activity, activity.getString(R.string.str_family_apply_toast2, names.get(0)));
        }
        resetDatas();
    }

    private void resetDatas() {
        List<MultiItemEntity> datas = new ArrayList<>();
        datas.addAll(mDatas);
        for (Integer id : ids) {
            for (MultiItemEntity item : datas) {
                if (item instanceof FamilyApplyUserInfo) {
                    FamilyApplyUserInfo info = (FamilyApplyUserInfo) item;
                    if (id == info.getId()) {
                        datas.remove(info);
                        break;
                    }
                }
            }
        }
        ids.clear();
        names.clear();
        mDatas.clear();
        mDatas.addAll(datas);
        mAdapter.notifyDataSetChanged();
        buttomShow();
    }


    public void initRresh(RefreshListenerAdapter reshListener) {
        initRreshView(R.id.refresh_view);
        initRreshHead();
        initButtom(false);
        if (refresh_view != null) {
            refresh_view.setOnRefreshListener(reshListener);
        }
    }
}
